package com.lizardmind.haofudoctor.ui.diagnosis.sickMan.MedicalHistory

import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lizardmind.haofudoctor.BR
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.other.MedicalHistoryBean

/**
 * Created by Administrator on 2017/10/24.
 * 病历adapter
 */
class MedicalHistoryAdapter(
        private val list:ArrayList<MedicalHistoryBean>,
        private val context: Context
                ):RecyclerView.Adapter<RecyclerView.ViewHolder>(){

    private val Empty=0;//emptyView
    private val HEADVIEW=10;//headView
    private val WenZhenDan=1;//问诊单item
    private val FuZhenDan=2;//复诊单item
    private val FangAn=3;//方案item

    private var isOnBind: OnBind?=null

    var headView: View? =null

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? {

        if (viewType==HEADVIEW&&headView!=null){
            return Holder1(this.headView!!)
        }
        if (viewType==WenZhenDan){
            return Holder1(LayoutInflater.from(context).inflate(R.layout.item_wen_zhen_dan,parent,false))
        }
        if (viewType==FuZhenDan){
            return Holder1(LayoutInflater.from(context).inflate(R.layout.item_fu_zhen_dan,parent,false))
        }
        if (viewType==FangAn){
            return Holder1(LayoutInflater.from(context).inflate(R.layout.item_fang_an,parent,false))
        }
        return Holder1(LayoutInflater.from(context).inflate(R.layout.empty_view_medical_history,parent,false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {

        if (getItemViewType(position)==HEADVIEW){
            return
        }

        val posi: Int=if (headView==null) position else (position-1)
        if (holder is Holder1 && list.size!=0){
            holder.getBinding().apply {
                setVariable(BR.item, list[posi])
            }
            isOnBind?.onBind(holder.getBinding(),posi,list[posi])
//            isOnBind?.onBind(holder.getBinding(),posi)
        }

    }


    override fun getItemCount(): Int {
        if (list.size!=0){
            return if (headView==null) list.size else list.size+1
        }else{
            return if (headView==null) 1 else 2
        }

    }

    override fun getItemViewType(position: Int): Int {

        if (position==0&&headView!=null){
            return HEADVIEW
        }else if (list.size!=0){
            return if (headView!=null) list[position-1].itemFlag else list[position].itemFlag
        }
        return Empty
    }

    fun setMyHeadView(headView: View){
        this.headView=headView
    }

    fun setOnBind(isOnBind: OnBind){
        this.isOnBind=isOnBind
    }

    internal inner class Holder1(view:View): RecyclerView.ViewHolder(view){
        private val bin: ViewDataBinding by lazy {
            DataBindingUtil.bind<ViewDataBinding>(itemView)
        }

        fun getBinding(): ViewDataBinding{
            return bin
        }

    }

    interface OnBind{
        fun onBind(itemBinding:ViewDataBinding,position: Int,bean:MedicalHistoryBean)
    }

}